/*
 */
#define _INTERFACE_CPP_

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

#include "interface_capture.h"


#define GLADE_HOOKUP_OBJECT(component,widget,name)			\
    g_object_set_data_full (G_OBJECT (component), name,			\
			    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name)	\
    g_object_set_data (G_OBJECT (component), name, widget)


GtkWidget*
create_window1 (void)
{
    GtkWidget *window1;

    GtkWidget *fixed_captura;
    GtkWidget *fixed_ajustes;


    GtkWidget *button_record;
    GtkWidget *button_exit;
    GtkWidget *label_record;
    GtkWidget *label_exit;
    GtkWidget *image_record;
    GtkWidget *alignment_record;
    GtkWidget *hbox1;
    GtkWidget *alignment_exit;
    GtkWidget *hbox4;

    GtkWidget *button_stop;
    GtkWidget *label_stop;
    GtkWidget *image_stop;
    GtkWidget *alignment_capturar;
    GtkWidget *hbox3;
    
    // Para la otra pestaña
    GtkWidget *label_shutter;
    GtkWidget *label_gain;
    GtkWidget *label_brightness;
    GtkWidget *label_light_intensity;
    GtkWidget *hscale_gain;
    GtkWidget *hscale_brightness;
    GtkWidget *hscale_shutter;
    GtkWidget *hscale_light_intensity;

    GtkWidget *checkbutton_external_sync;
    GtkWidget *checkbutton_cam_sync;
    GtkWidget *checkbutton_sim_sync;
    //  GtkWidget *checkbutton_offset;


#define H_INI 25
#define V_INI 15
#define BUTTON_HEIGHT 35
#define BUTTON_WIDTH 110


#define labels_voffset 23
#define labels_space 62
#define slider_voffset 15
#define sliders_width 300

#define checkbox_voffset 20

    int v=V_INI, h=H_INI;


    window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window1), _("FICOSA"));
    
    fixed_captura = gtk_fixed_new ();
    gtk_widget_show (fixed_captura);
    gtk_container_add (GTK_CONTAINER (window1), fixed_captura);
    gtk_widget_set_size_request (fixed_captura, 370, 440);
    
    button_record = gtk_button_new ();
    gtk_widget_show (button_record);
    gtk_fixed_put (GTK_FIXED (fixed_captura), button_record, h, v);
    gtk_widget_set_size_request (button_record, BUTTON_WIDTH, BUTTON_HEIGHT);
    GTK_WIDGET_SET_FLAGS (button_record, GTK_CAN_DEFAULT);
    gtk_button_set_relief (GTK_BUTTON (button_record), GTK_RELIEF_NORMAL);

    alignment_record = gtk_alignment_new (0.5, 0.5, 0, 0);
    gtk_widget_show (alignment_record);
    gtk_container_add (GTK_CONTAINER (button_record), alignment_record);

    hbox1 = gtk_hbox_new (false, 2);
    gtk_widget_show (hbox1);
    gtk_container_add (GTK_CONTAINER (alignment_record), hbox1);

    image_record = gtk_image_new_from_stock ("gtk-media-record", GTK_ICON_SIZE_BUTTON);
    gtk_widget_show (image_record);
    gtk_box_pack_start (GTK_BOX (hbox1), image_record, false, false, 0);

    label_record = gtk_label_new_with_mnemonic (_("_Start"));
    gtk_widget_show (label_record);
    gtk_box_pack_start (GTK_BOX (hbox1), label_record, false, false, 0);

    button_stop = gtk_button_new ();
    gtk_widget_show (button_stop);
    gtk_fixed_put (GTK_FIXED (fixed_captura), button_stop, h +BUTTON_WIDTH + 20, v);
    gtk_widget_set_size_request (button_stop, BUTTON_WIDTH, BUTTON_HEIGHT);

    alignment_capturar = gtk_alignment_new (0.5, 0.5, 0, 0);
    gtk_widget_show (alignment_capturar);
    gtk_container_add (GTK_CONTAINER (button_stop), alignment_capturar);

    hbox3 = gtk_hbox_new (false, 2);
    gtk_widget_show (hbox3);
    gtk_container_add (GTK_CONTAINER (alignment_capturar), hbox3);

    image_stop = gtk_image_new_from_stock ("gtk-stop", GTK_ICON_SIZE_BUTTON);
    gtk_widget_show (image_stop);
    gtk_box_pack_start (GTK_BOX (hbox3), image_stop, false, false, 0);

    label_stop = gtk_label_new_with_mnemonic (_("_Stop"));
    gtk_widget_show (label_stop);
    gtk_box_pack_start (GTK_BOX (hbox3), label_stop, false, false, 0);

    label_file_name = gtk_label_new (_(" "));
    gtk_widget_show (label_file_name);
    gtk_fixed_put (GTK_FIXED (fixed_captura), label_file_name, 20, 400);
    gtk_widget_set_size_request (label_file_name, 300, 17);


    // Pestaña de ajuste de la cámara.
    //
    fixed_ajustes = fixed_captura;

    v += BUTTON_HEIGHT + labels_voffset;
    int v2 = v;

    label_shutter = gtk_label_new_with_mnemonic (_("Shutter"));
    gtk_widget_show (label_shutter);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), label_shutter, 150, v);
    gtk_widget_set_size_request (label_shutter, 64, 16);
    gtk_label_set_use_markup (GTK_LABEL (label_shutter), true);

    v += labels_space;
    label_gain = gtk_label_new_with_mnemonic (_("Gain"));
    gtk_widget_show (label_gain);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), label_gain, 160, v);
    gtk_widget_set_size_request (label_gain, 46, 17);
    gtk_label_set_use_markup (GTK_LABEL (label_gain), true);

    v += labels_space;
    label_brightness = gtk_label_new_with_mnemonic (_("Brightness"));
    gtk_widget_show (label_brightness);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), label_brightness, 140, v);
    gtk_widget_set_size_request (label_brightness, 88, 16);
    gtk_label_set_use_markup (GTK_LABEL (label_brightness), true);

    v += labels_space;
    label_light_intensity = gtk_label_new_with_mnemonic (_("Intensidad de Iluminacion (%)"));
    gtk_widget_show (label_light_intensity);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), label_light_intensity, 100, v);
    gtk_widget_set_size_request (label_light_intensity, 200, 17);
    gtk_label_set_use_markup (GTK_LABEL (label_light_intensity), true);

    v = v2 + slider_voffset;
    hscale_shutter = 
	gtk_hscale_new (GTK_ADJUSTMENT (gtk_adjustment_new (shutter_v, 5, 4095, 50, 50, 0)));
    gtk_widget_show (hscale_shutter);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), hscale_shutter, 25, v);
    gtk_widget_set_size_request (hscale_shutter, sliders_width, 40);
    gtk_scale_set_digits (GTK_SCALE (hscale_shutter), 0);

    v += labels_space;
    hscale_gain = 
	gtk_hscale_new (GTK_ADJUSTMENT (gtk_adjustment_new (gain_value, 192, 1023, 10, 0, 0)));
    gtk_widget_show (hscale_gain);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), hscale_gain, 25, v);
    gtk_widget_set_size_request (hscale_gain, sliders_width, 40);
    gtk_scale_set_digits (GTK_SCALE (hscale_gain), 0);

    v += labels_space;
    hscale_brightness = 
	gtk_hscale_new (GTK_ADJUSTMENT (gtk_adjustment_new (brightness_value, 0, 255, 1, 0, 0)));
    gtk_widget_show (hscale_brightness);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), hscale_brightness, 25, v);
    gtk_widget_set_size_request (hscale_brightness, sliders_width, 40);
    gtk_scale_set_digits (GTK_SCALE (hscale_brightness), 0);

    v += labels_space;
    hscale_light_intensity = 
	gtk_hscale_new (GTK_ADJUSTMENT (gtk_adjustment_new (light_intensity_value, 0, 100, 2, 0, 0)));
    gtk_widget_show (hscale_light_intensity);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), hscale_light_intensity, 25, v);
    gtk_widget_set_size_request (hscale_light_intensity, sliders_width, 40);
    gtk_scale_set_digits (GTK_SCALE (hscale_light_intensity), 0);


    v += labels_space;
    v2 = v;
    checkbutton_external_sync = gtk_check_button_new_with_mnemonic (_("Sincronismo Externo"));
    gtk_widget_show (checkbutton_external_sync);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), checkbutton_external_sync, h, v);
    gtk_widget_set_size_request (checkbutton_external_sync, 155, 22);

    v += checkbox_voffset;

    checkbutton_cam_sync = gtk_check_button_new_with_mnemonic (_("Sincronismo Interno"));
    gtk_widget_show (checkbutton_cam_sync);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), checkbutton_cam_sync, h, v);
    gtk_widget_set_size_request (checkbutton_cam_sync, 155, 22);

    v += checkbox_voffset;
    checkbutton_sim_sync = gtk_check_button_new_with_mnemonic (_("Simular sincronismo"));
    gtk_widget_show (checkbutton_sim_sync);
    gtk_fixed_put (GTK_FIXED (fixed_ajustes), checkbutton_sim_sync, h, v);
    gtk_widget_set_size_request (checkbutton_sim_sync, 155, 22);


    v = v2-10;
    button_exit = gtk_button_new ();
    gtk_widget_show (button_exit);
    gtk_fixed_put (GTK_FIXED (fixed_captura), button_exit, h + sliders_width-BUTTON_WIDTH, v);
    gtk_widget_set_size_request (button_exit, BUTTON_WIDTH, BUTTON_HEIGHT);
    GTK_WIDGET_SET_FLAGS (button_exit, GTK_CAN_DEFAULT);
    gtk_button_set_relief (GTK_BUTTON (button_exit), GTK_RELIEF_NORMAL);

    alignment_exit = gtk_alignment_new (0.5, 0.5, 0, 0);
    gtk_widget_show (alignment_exit);
    gtk_container_add (GTK_CONTAINER (button_exit), alignment_exit);

    hbox4 = gtk_hbox_new (false, 2);
    gtk_widget_show (hbox4);
    gtk_container_add (GTK_CONTAINER (alignment_exit), hbox4);

    label_exit = gtk_label_new_with_mnemonic (_("E_xit"));
    gtk_widget_show (label_exit);
    gtk_box_pack_start (GTK_BOX (hbox4), label_exit, false, false, 0);


    // Callback functions
    //
    g_signal_connect ((gpointer) window1, "destroy",
		      G_CALLBACK (on_salir_activate),
		      NULL);

    g_signal_connect ((gpointer) button_record, "clicked",
		      G_CALLBACK (on_grabar_clicked),
		      (gpointer) checkbutton_external_sync);

    g_signal_connect ((gpointer) button_exit, "clicked",
		      G_CALLBACK (on_salir_activate), //(on_exit_clicked),
		      (gpointer) checkbutton_external_sync);

    g_signal_connect ((gpointer) button_stop, "clicked",
		      G_CALLBACK (on_stop_clicked),
		      (gpointer) checkbutton_external_sync);

    g_signal_connect ((gpointer) hscale_gain, "value_changed",
		      G_CALLBACK (on_hscale_gain),
		      NULL);

    g_signal_connect ((gpointer) hscale_brightness, "value_changed",
		      G_CALLBACK (on_hscale_brightness),
		      NULL);

    g_signal_connect ((gpointer) hscale_shutter, "value_changed",
		      G_CALLBACK (on_hscale_shutter),
		      NULL);

    g_signal_connect ((gpointer) hscale_light_intensity, "value_changed",
		      G_CALLBACK (on_hscale_light_intensity),
		      NULL);

    g_signal_connect ((gpointer) checkbutton_external_sync, "clicked",
		      G_CALLBACK (on_checkbutton_sync_external_activate),
		      (gpointer) checkbutton_cam_sync);

    g_signal_connect ((gpointer) checkbutton_cam_sync, "clicked",
		      G_CALLBACK (on_checkbutton_sync_camera_activate),
		      NULL);

    g_signal_connect ((gpointer) checkbutton_sim_sync, "clicked",
		      G_CALLBACK (on_checkbutton_sim_sync),
		      NULL);

    //
    //  g_signal_connect ((gpointer) checkbutton_offset, "clicked",
    //                    G_CALLBACK (on_checkbutton_offset),
    //                    NULL);

    /* Store pointers to all widgets, for use by lookup_widget(). */
    GLADE_HOOKUP_OBJECT (window1, window1, "window1");

    GLADE_HOOKUP_OBJECT (window1, fixed_captura, "fixed_captura");
    GLADE_HOOKUP_OBJECT (window1, button_record, "button_record");
    GLADE_HOOKUP_OBJECT (window1, button_exit, "button_exit");
    GLADE_HOOKUP_OBJECT (window1, alignment_record, "alignment_record");
    GLADE_HOOKUP_OBJECT (window1, hbox1, "hbox1");
    GLADE_HOOKUP_OBJECT (window1, image_record, "image_record");
    GLADE_HOOKUP_OBJECT (window1, label_record, "label_record");
    GLADE_HOOKUP_OBJECT (window1, button_stop, "button_stop");
    GLADE_HOOKUP_OBJECT (window1, alignment_capturar, "alignment_capturar");
    GLADE_HOOKUP_OBJECT (window1, hbox3, "hbox3");
    GLADE_HOOKUP_OBJECT (window1, image_stop, "image_stop");
    GLADE_HOOKUP_OBJECT (window1, label_stop, "label_stop");
    GLADE_HOOKUP_OBJECT (window1, checkbutton_cam_sync, "checkbutton_cam_sync");
    GLADE_HOOKUP_OBJECT (window1, checkbutton_external_sync, "checkbutton_external_sync");
    GLADE_HOOKUP_OBJECT (window1, checkbutton_sim_sync, "checkbutton_sim_sync");

    return window1;


}



